<?php

require_once '../MMysql.php';
require_once("../../mobile/SendTemplateSMS.php");
require_once("../../phpmailer/send.php");

/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 16/8/18
 * Time: 上午9:17
 */
class messagefuction
{
    public function addmessage($message_fullname,$message_email,$message_tel,$message_wechat,$message_address,
                                $message_city,$message_property_like,$message_lowest,$message_highest)
    {

        $mysql = new MMysql();
        $flag= $mysql->insert('message',array(
            'message_fullname'=>$message_fullname,
            'message_email'=>$message_email,
            'message_tel'=>$message_tel,
            'message_wechat'=>$message_wechat,
            'message_address'=>$message_address,
            'message_city'=>$message_city,
            'message_property_like'=>$message_property_like,
            'message_lowest'=>$message_lowest,
            'message_highest'=>$message_highest,
            'del_flag'=>0,
            'create_time'=>date('y-m-d',time())
        ));
        if($flag==1){

            $user= $mysql
//                ->where(array('account'=>'admin','del_flag'=>0))
                ->select('user');
            //邮箱通知
            sendMail($user[0]['user_email'],'DMI','投资人'.$message_fullname.'希望你替他做房产投资咨询。请尽快进入后台管理端查看或联系投资人吧。<br/>
            投资人邮箱：'.$message_email.'
<br/>投资人联系电话：'.$message_tel.'
<br/>投资人微信：'.$message_wechat.'
<br/>投资人目前住址：'.$message_address.'
<br/>投资人感兴趣的城市是'.$message_city.'
<br/>投资人喜欢的房产是这样的'.$message_property_like.'
<br/>投资人希望的房产价格范围是：'.$message_lowest.'-'.$message_highest.'。<br/>点击链接进入后台管理端：www.dymondmynes.com');

            //手机通知
            sendTemplateSMS("15869167240",array($message_fullname),"120191");

            echo 1;
        }
        else{
            echo 0;
        }
    }

    public function selectmessage($page,$rows)
    {

        $mysql = new MMysql();
        $res = $mysql
            ->where(array("del_flag"=>0))
            ->order(array('message_id'=>'desc'))
            ->limit($page,$rows)
            ->select('message');

        $count=$mysql->where(array("del_flag"=>0))->count('message');

        $array=Array();
        $array["rows"]=$res;
        $array["total"]=$count;
        echo json_encode($array);

    }
}


$method =$_REQUEST["method"];

if($method=='list'){

    $d=new messagefuction();

    return $d->selectmessage($_REQUEST["page"],$_REQUEST["rows"]);

}
else if($method=='add'){
    $d=new messagefuction();

//    echo $_REQUEST["parameter_value"];

    echo $d->addmessage(
        $_REQUEST["message_fullname"],$_REQUEST["message_email"],$_REQUEST["message_tel"],
        $_REQUEST["message_wechat"],$_REQUEST["message_address"],$_REQUEST["message_city"],
        $_REQUEST["message_property_like"],$_REQUEST["message_lowest"],$_REQUEST["message_highest"]);
}
